package com.tank.collision;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.tank.constant.Config;
import com.tank.constant.GameType;

import java.util.List;

public class BulletTerrainCollideHandler extends CollisionHandler {
    public BulletTerrainCollideHandler(Object a, Object b) {
        super(a, b);
    }

    @Override
    protected void onCollision(Entity bullet, Entity b) {
        List<Entity> collidedEntities = FXGL.getGameWorld().getEntitiesFiltered(entity -> bullet.isColliding(entity) && (GameType.BRICK == entity.getType() || GameType.STONE == entity.getType() || GameType.GREENS == entity.getType()));
        int bulletLevel = bullet.getInt(Config.BULLET_LEVEL_KEY);
        boolean removeBullet = false;
        for (Entity entity : collidedEntities) {
            if (GameType.BRICK == entity.getType()) {
                FXGL.spawn("explosion", bullet.getCenter().subtract(50 / 2.0, 50 / 2.0));
                entity.removeFromWorld();
                removeBullet = true;
            } else if (GameType.STONE == entity.getType()) {
                FXGL.spawn("explosion", bullet.getCenter().subtract(50 / 2.0, 50 / 2.0));
                if (Config.MAX_TANK_LEVEL == bulletLevel) {
                    entity.removeFromWorld();
                }
                removeBullet = true;
            } else if (GameType.GREENS == entity.getType()) {
                if (Config.MAX_TANK_LEVEL == bulletLevel) {
                    entity.removeFromWorld();
                }
            }
        }
        if (removeBullet) {
            bullet.removeFromWorld();
        }
    }
}
